<?php

class TaskOrdersCsv{
	
	//原生数据
	private $data = array();
	//每一行
	private $lines = array();
	
	private $data_list = array();
	
	//每一行
	private $line_count = 0;
	
	function __construct(){
		
	}
	
	//数组转
	function array_to_data($array){
		
		import('@.ORG.Shipping');
			
		$this->data = $array;
		$this->line_count = count($this->data);

		
		$cShipping = new Shipping();
		$shippings = $cShipping->shipping_name_map();
		
		
		$error_line = array();
		
		foreach($this->data as $i => $d){
			
			
			$shipping_id = 0;
			if(isset($shippings[$d[0]])){
				$shipping_id = $shippings[$d[0]];	
			}else{
				if(!empty($d[0])){
					//为空时跳过
					$error_line[] = $i + 2;
				}
			}
			
			//合并
			$this->lines[] = array(
				'shipping_id' 	=> $shipping_id,
				'shipping_name' => $d[0],
				'invoice_no' 	=> $d[1],
				'consignee' 	=> $d[2],
				'address' 		=> $d[3],
				'mobile_phone' 	=> $d[4],
				'goods_url' 	=> $d[5],
				'goods_name' 	=> $d[6],
				'keywords' 		=> $d[7],
				'goods_number' 	=> $d[8],
				'goods_price' 	=> $d[9],
				'final_price' 	=> $d[10],
				'comment' 		=> $d[11]
			);
		}
		
		
		if(!empty($error_line)){
			$msg = implode(',', $error_line).'行快递公司未找到匹配，请检查';
			redirect('/index.php/Admin/Task/add','2',$msg);
		}
			
		return $this->lines;
	}
	
	function unserialize($string){
		
		return unserialize($string);
	}
	//序列化转数据
	function serializable_to_data($string){

		return $this->array_to_data($this->unserialize($string));
	}
	
	//转换成字段对应的数组
	function convert_to_lines($serializable){
		
		 return $this->serializable_to_data($serializable);
		 
	}
	
	//汇总
	function summary($data){
		
		//去首行标题
		$task_order_money 	= 0;
		$order_count		= count($data);
		
		foreach ($data as $d){
			$task_order_money += $d['final_price'];
		}
			
		return array(
			'task_order_money' 	=> $task_order_money,
			'order_count' 		=> $order_count
		);
	}
}

?>